package com.lws.basic.base;

import android.app.Dialog;
import android.os.Bundle;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.lws.basic.R;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.UIUtils;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import io.reactivex.annotations.NonNull;

/**
 * 对话框的基类
 */

public abstract class BaseDialogFragment extends DialogFragment {
    private boolean isCanceled;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity(), R.style.Theme_AppCompat_NoActionBar);
        // 关闭标题栏，setContentView() 之前调用
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(initView());
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        initWindow(window);

        return dialog;
    }

    /**
     * 是或否
     * @param canceled
     */
    private void setCanceled(boolean canceled){
        isCanceled = canceled;

    }


    private View initView() {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(setLayoutId(), null);
        initData(view);
        return view;
    }


    /**
     * @param window  窗体
     * @param gravity 对齐方式  {@link android.view.Gravity#CENTER}
     * @param width  宽度         {@link WindowManager.LayoutParams#WRAP_CONTENT}
     * @param height 高度
     * @param x   左上角x轴偏移量
     * @param y   左上角y轴偏移量
     */
    protected void setLayoutParams(Window window, int gravity,int width,int height,int x,int y) {

        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = gravity;
        layoutParams.width = width == WindowManager.LayoutParams.WRAP_CONTENT
                || width == WindowManager.LayoutParams.MATCH_PARENT
                ? width: UIUtils.dip2Px(getContext(),width);

        layoutParams.height = height == WindowManager.LayoutParams.WRAP_CONTENT
                || height == WindowManager.LayoutParams.MATCH_PARENT
                ? height:UIUtils.dip2Px(getContext(),height);
        layoutParams.y = UIUtils.dip2Px(getContext(),y);
        //适配华为软键盘
        if (gravity == Gravity.BOTTOM && height!= WindowManager.LayoutParams.WRAP_CONTENT &&
                height != WindowManager.LayoutParams.MATCH_PARENT) {
            layoutParams.y = layoutParams.y + UIUtils.getDaoHangHeight(getContext());
        }

        setDefaultBackground(window, R.color.transparent);


    }

    @NonNull
    protected void setDefaultBackground(Window window, int resId) {
        window.setBackgroundDrawableResource(resId);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.dimAmount = 0.3f;//使背景变暗
        window.setAttributes(layoutParams);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//使背景变暗
        window.setAttributes(layoutParams);
    }

    /**
     * @return
     * 导入布局
     */
    public abstract int setLayoutId();

    /**
     * @param window 设置window的参数参数
     * 一般调用{@link #setLayoutParams(Window, int, int, int, int, int)}方法
     */
    protected abstract void initWindow(Window window);


    protected void initData(View rootView) {
    }


    @Override
    public void show(FragmentManager manager, String tag) {
       /* mDismissed = false;
        mShownByMe = true;*/

            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            // 这里吧原来的commit()方法换成了commitAllowingStateLoss()
            ft.commitAllowingStateLoss();

    }

}
